package com.wthink.hzgj.util

import java.util.concurrent.TimeUnit

object Dial2box {

    fun start() {
        Thread {
            while (true) {
                dial()
                TimeUnit.SECONDS.sleep(1)
            }
        }.start()
    }

        private fun dial() {
            Log.debug("begin dial by pppd")

            try {
    //            val pb = ProcessBuilder("/system/xbin/su")
    //            pb.redirectErrorStream(true)
    //            val proc = pb.start()
                val proc = Runtime.getRuntime().exec("/system/xbin/su")

                proc.outputStream.write("pppd nodetach noauth noipdefault mtu 500  idle 300 local /dev/ttyS0 115200 10.0.0.1:10.0.0.2\n".toByteArray())
                proc.outputStream.flush()

                if (proc.inputStream.available() > 0) {
                    val rsp = ByteArray(proc.inputStream.available())
                    proc.inputStream.read(rsp)
                    Log.debug(rsp.toString(Charsets.UTF_8))
                }

                proc.waitFor()
            } catch (err: Throwable) {
                Log.debug("ppp err", err)
            }
        }
}